package cz.czu.pef.DataModelling.ejb.impl;

import java.util.Collection;
import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cz.czu.pef.DataModelling.dao.Dao;
import cz.czu.pef.DataModelling.dao.impl.CustomFilterField;
import cz.czu.pef.DataModelling.dao.impl.CustomSortField;
import cz.czu.pef.DataModelling.ejb.api.AbstractWrapperService;

@Component
public abstract class AbstractWrapperServiceImpl<T> implements AbstractWrapperService<T>
{
	public AbstractWrapperServiceImpl()
	{
	}

	public abstract Dao<T> getDao();

	@Override
	@Transactional(timeout=120, propagation=Propagation.REQUIRED, readOnly=false)
	public T save(T entity)
	{
		return getDao().save(entity);
	}

	@Override
	@Transactional(timeout=120, propagation=Propagation.REQUIRED, readOnly=true)
	public T getByStringId(String id)
	{
		return getDao().getByStringId(id);
	}

	@Override
	@Transactional(timeout=120, propagation=Propagation.REQUIRED, readOnly=false)
	public void saveOrUpdate(T entity)
	{
		getDao().saveOrUpdate(entity);
	}

	@Override
	@Transactional(timeout=120, propagation=Propagation.REQUIRED, readOnly=true)
	public T getById(long id)
	{
		return getDao().getById(id);
	}

	@Override
	@Transactional(timeout=120, propagation=Propagation.REQUIRED, readOnly=false)
	public T getByIdLocked(long id)
	{
		return getDao().getByIdLocked(id);
	}

	@Override
	@Transactional(timeout=120, propagation=Propagation.REQUIRED, readOnly=false)
	public void update(T entity)
	{
		getDao().update(entity);
	}

	@Override
	@Transactional(timeout=120, propagation=Propagation.REQUIRED, readOnly=false)
	public void delete(T entity)
	{
		getDao().delete(entity);
	}

	@Override
	@Transactional(timeout=120, propagation=Propagation.REQUIRED, readOnly=false)
	public void deleteById(String idValue)
	{
		getDao().deleteById(idValue);
	}

	@Override
	@Transactional(timeout=120, propagation=Propagation.REQUIRED, readOnly=false)
	public T merge(T entity)
	{
		return getDao().merge(entity);
	}

	@Override
	@Transactional(timeout=120, propagation=Propagation.REQUIRED, readOnly=true)
	public Long count()
	{
		return getDao().count();
	}

	@Override
	@Transactional(timeout=120, propagation=Propagation.REQUIRED, readOnly=true)
	public Long countWithFilter(List<CustomFilterField> filterFields)
	{
		return getDao().countWithFilter(filterFields);
	}

	@Override
	@Transactional(timeout=120, propagation=Propagation.REQUIRED, readOnly=true)
	public List<T> findByAttributeIn(String attribName, Collection<? extends Object> value)
	{
		return getDao().findByAttributeIn(attribName, value);
	}

	@Override
	@Transactional(timeout=120, propagation=Propagation.REQUIRED, readOnly=true)
	public List<T> findByAttribute(String attribName, Object value)
	{
		return getDao().findByAttribute(attribName, value);
	}

	@Override
	@Transactional(timeout=120, propagation=Propagation.REQUIRED, readOnly=true)
	public T findByAttributeUniqueResult(String attribName, Object value)
	{
		return getDao().findByAttributeUniqueResult(attribName, value);
	}

	@Override
	@Transactional(timeout=120, propagation=Propagation.REQUIRED, readOnly=true)
	public List<T> findAll()
	{
		return getDao().findAll();
	}

	@Override
	@Transactional(timeout=120, propagation=Propagation.REQUIRED, readOnly=true)
	public List<T> findAllByRange(int firstRow, int endRow)
	{
		return getDao().findAllByRange(firstRow, endRow);
	}

	@Override
	@Transactional(timeout=120, propagation=Propagation.REQUIRED, readOnly=true)
	public List<T> findAllByRangeWithFilter(int firstRow, int endRow, List<CustomFilterField> filterFields)
	{
		return getDao().findAllByRangeWithFilter(firstRow, endRow, filterFields);
	}

	@Override
	@Transactional(timeout=120, propagation=Propagation.REQUIRED, readOnly=true)
	public List<T> findAllByRangeWithFilterAndSort(int firstRow, int endRow, List<CustomFilterField> filterFields, List<CustomSortField> sortFields)
	{
		return getDao().findAllByRangeWithFilterAndSort(firstRow, endRow, filterFields, sortFields);
	}
}
